package com.designpattern.factory;

/**
 * 项目名称: java_algorithm
 * @ClassName FactoryMethod
 * @Description
 * @Author huipanxing huipanxing@chinasofti.com   @Date 2021/9/7 10:35
 */
public class FactoryMethod {
    public static void main(String[] args) {
//        Application application = new Application();
//        Product product = application.getObject("1");
//        product.method1();

        Application application=new ConcreteProductA();
        Product product = application.createProduct();
        product.method1();
    }
}

interface Product {
    void method1();
}

class ProductA implements Product {
    @Override
    public void method1() {
        System.out.println("ProductA.method1 executed.");
    }
}

class ProductB implements Product {
    @Override
    public void method1() {
        System.out.println("ProductB.method1 executed.");
    }
}

class SimpleFactory {
    public static Product createProduct(String type) {
        if (type.equals("0")) {
            return new ProductA();
        } else if (type.equals("1")) {
            return new ProductB();
        } else {
            return null;
        }
    }
}

abstract class Application {
    abstract Product createProduct();

    Product getObject() {
        Product product = createProduct();
        return product;
    }
}

class ConcreteProductA extends Application{

    @Override
    Product createProduct() {
        return new ProductA();
    }
}

class ConcreteProductB extends Application{

    @Override
    Product createProduct() {
        return new ProductB();
    }
}